package jffile;

/*
 * Site.java
 *
 * Created on Jan 2, 2011, 9:44:13 PM
 *
 * @author pquiring
 */

import java.io.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.table.*;

import javaforce.*;
import javaforce.jbus.*;
import javaforce.utils.*;

public class Site extends javax.swing.JPanel implements JFileBrowserListener {

  /**
   * Creates new form Site
   */
  public Site() {
    initComponents();
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    localFileMenu = new javax.swing.JPopupMenu();
    local_upload = new javax.swing.JMenuItem();
    local_sep_1 = new javax.swing.JPopupMenu.Separator();
    local_open = new javax.swing.JMenuItem();
    local_edit = new javax.swing.JMenuItem();
    local_mkdir = new javax.swing.JMenuItem();
    local_refresh = new javax.swing.JMenuItem();
    local_sep_2 = new javax.swing.JPopupMenu.Separator();
    local_trash = new javax.swing.JMenuItem();
    local_delete = new javax.swing.JMenuItem();
    local_rename = new javax.swing.JMenuItem();
    local_sep_3 = new javax.swing.JPopupMenu.Separator();
    local_props = new javax.swing.JMenuItem();
    local_sep_4 = new javax.swing.JPopupMenu.Separator();
    local_cut = new javax.swing.JMenuItem();
    local_copy = new javax.swing.JMenuItem();
    local_paste = new javax.swing.JMenuItem();
    local_sep_5 = new javax.swing.JPopupMenu.Separator();
    local_share = new javax.swing.JMenuItem();
    remoteFileMenu = new javax.swing.JPopupMenu();
    remote_download = new javax.swing.JMenuItem();
    remote_sep_1 = new javax.swing.JPopupMenu.Separator();
    remote_open = new javax.swing.JMenuItem();
    remote_edit = new javax.swing.JMenuItem();
    remote_mkdir = new javax.swing.JMenuItem();
    remote_refresh = new javax.swing.JMenuItem();
    remote_sep_2 = new javax.swing.JPopupMenu.Separator();
    remote_trash = new javax.swing.JMenuItem();
    remote_delete = new javax.swing.JMenuItem();
    remote_rename = new javax.swing.JMenuItem();
    remote_sep_3 = new javax.swing.JPopupMenu.Separator();
    remote_props = new javax.swing.JMenuItem();
    remote_sep_4 = new javax.swing.JPopupMenu.Separator();
    remote_cut = new javax.swing.JMenuItem();
    remote_copy = new javax.swing.JMenuItem();
    remote_paste = new javax.swing.JMenuItem();
    remote_sep_5 = new javax.swing.JPopupMenu.Separator();
    remote_share = new javax.swing.JMenuItem();
    localDesktopMenu = new javax.swing.JPopupMenu();
    local_mkdir_2 = new javax.swing.JMenuItem();
    local_refresh_2 = new javax.swing.JMenuItem();
    local_paste_2 = new javax.swing.JMenuItem();
    remoteDesktopMenu = new javax.swing.JPopupMenu();
    remote_mkdir_2 = new javax.swing.JMenuItem();
    remote_refresh_2 = new javax.swing.JMenuItem();
    remote_paste_2 = new javax.swing.JMenuItem();
    localSide = new javax.swing.JPanel();
    localPanel = new javax.swing.JPanel();
    jToolBar1 = new javax.swing.JToolBar();
    localLabel = new javax.swing.JLabel();
    local_cdup = new javax.swing.JButton();
    localDir = new javax.swing.JTextField();
    remoteSide = new javax.swing.JPanel();
    remotePanel = new javax.swing.JPanel();
    jToolBar2 = new javax.swing.JToolBar();
    remoteLabel = new javax.swing.JLabel();
    remote_cdup = new javax.swing.JButton();
    remoteDir = new javax.swing.JTextField();
    localTreeScroll = new javax.swing.JScrollPane();
    localTree = new javax.swing.JTree();
    remoteTreeScroll = new javax.swing.JScrollPane();
    remoteTree = new javax.swing.JTree();

    local_upload.setText("Upload");
    local_upload.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        local_uploadActionPerformed(evt);
      }
    });
    localFileMenu.add(local_upload);
    localFileMenu.add(local_sep_1);

    local_open.setText("Open");
    local_open.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        local_openActionPerformed(evt);
      }
    });
    localFileMenu.add(local_open);

    local_edit.setText("Edit");
    local_edit.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        local_editActionPerformed(evt);
      }
    });
    localFileMenu.add(local_edit);

    local_mkdir.setText("Create Directory");
    local_mkdir.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        local_mkdirActionPerformed(evt);
      }
    });
    localFileMenu.add(local_mkdir);

    local_refresh.setText("Refresh");
    local_refresh.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        local_refreshActionPerformed(evt);
      }
    });
    localFileMenu.add(local_refresh);
    localFileMenu.add(local_sep_2);

    local_trash.setText("Trash");
    local_trash.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        local_trashActionPerformed(evt);
      }
    });
    localFileMenu.add(local_trash);

    local_delete.setText("Delete");
    local_delete.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        local_deleteActionPerformed(evt);
      }
    });
    localFileMenu.add(local_delete);

    local_rename.setText("Rename");
    local_rename.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        local_renameActionPerformed(evt);
      }
    });
    localFileMenu.add(local_rename);
    localFileMenu.add(local_sep_3);

    local_props.setText("Properties");
    local_props.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        local_propsActionPerformed(evt);
      }
    });
    localFileMenu.add(local_props);
    localFileMenu.add(local_sep_4);

    local_cut.setText("Cut");
    local_cut.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        local_cutActionPerformed(evt);
      }
    });
    localFileMenu.add(local_cut);

    local_copy.setText("Copy");
    local_copy.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        local_copyActionPerformed(evt);
      }
    });
    localFileMenu.add(local_copy);

    local_paste.setText("Paste");
    local_paste.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        local_pasteActionPerformed(evt);
      }
    });
    localFileMenu.add(local_paste);
    localFileMenu.add(local_sep_5);

    local_share.setText("Share");
    local_share.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        local_shareActionPerformed(evt);
      }
    });
    localFileMenu.add(local_share);

    remote_download.setText("Download");
    remote_download.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        remote_downloadActionPerformed(evt);
      }
    });
    remoteFileMenu.add(remote_download);
    remoteFileMenu.add(remote_sep_1);

    remote_open.setText("Open");
    remote_open.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        remote_openActionPerformed(evt);
      }
    });
    remoteFileMenu.add(remote_open);

    remote_edit.setText("Edit");
    remote_edit.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        remote_editActionPerformed(evt);
      }
    });
    remoteFileMenu.add(remote_edit);

    remote_mkdir.setText("Create Directory");
    remote_mkdir.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        remote_mkdirActionPerformed(evt);
      }
    });
    remoteFileMenu.add(remote_mkdir);

    remote_refresh.setText("Refresh");
    remote_refresh.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        remote_refreshActionPerformed(evt);
      }
    });
    remoteFileMenu.add(remote_refresh);
    remoteFileMenu.add(remote_sep_2);

    remote_trash.setText("Trash");
    remote_trash.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        remote_trashActionPerformed(evt);
      }
    });
    remoteFileMenu.add(remote_trash);

    remote_delete.setText("Delete");
    remote_delete.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        remote_deleteActionPerformed(evt);
      }
    });
    remoteFileMenu.add(remote_delete);

    remote_rename.setText("Rename");
    remote_rename.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        remote_renameActionPerformed(evt);
      }
    });
    remoteFileMenu.add(remote_rename);
    remoteFileMenu.add(remote_sep_3);

    remote_props.setText("Properties");
    remote_props.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        remote_propsActionPerformed(evt);
      }
    });
    remoteFileMenu.add(remote_props);
    remoteFileMenu.add(remote_sep_4);

    remote_cut.setText("Cut");
    remote_cut.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        remote_cutActionPerformed(evt);
      }
    });
    remoteFileMenu.add(remote_cut);

    remote_copy.setText("Copy");
    remote_copy.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        remote_copyActionPerformed(evt);
      }
    });
    remoteFileMenu.add(remote_copy);

    remote_paste.setText("Paste");
    remote_paste.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        remote_pasteActionPerformed(evt);
      }
    });
    remoteFileMenu.add(remote_paste);
    remoteFileMenu.add(remote_sep_5);

    remote_share.setText("Share");
    remote_share.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        remote_shareActionPerformed(evt);
      }
    });
    remoteFileMenu.add(remote_share);

    local_mkdir_2.setText("New Folder");
    local_mkdir_2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        local_mkdir_2ActionPerformed(evt);
      }
    });
    localDesktopMenu.add(local_mkdir_2);

    local_refresh_2.setText("Refresh");
    local_refresh_2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        local_refresh_2ActionPerformed(evt);
      }
    });
    localDesktopMenu.add(local_refresh_2);

    local_paste_2.setText("Paste");
    local_paste_2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        local_paste_2ActionPerformed(evt);
      }
    });
    localDesktopMenu.add(local_paste_2);

    remote_mkdir_2.setText("New Folder");
    remote_mkdir_2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        remote_mkdir_2ActionPerformed(evt);
      }
    });
    remoteDesktopMenu.add(remote_mkdir_2);

    remote_refresh_2.setText("Refresh");
    remote_refresh_2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        remote_refresh_2ActionPerformed(evt);
      }
    });
    remoteDesktopMenu.add(remote_refresh_2);

    remote_paste_2.setText("Paste");
    remote_paste_2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        remote_paste_2ActionPerformed(evt);
      }
    });
    remoteDesktopMenu.add(remote_paste_2);

    setLayout(null);

    localSide.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

    localPanel.setLayout(new java.awt.GridLayout(1, 1));

    jToolBar1.setFloatable(false);
    jToolBar1.setRollover(true);

    localLabel.setText("Folder:");
    jToolBar1.add(localLabel);

    local_cdup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jffile/jfile-up.png"))); // NOI18N
    local_cdup.setFocusable(false);
    local_cdup.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    local_cdup.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    local_cdup.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        local_cdupActionPerformed(evt);
      }
    });
    jToolBar1.add(local_cdup);

    localDir.setText("/");
    localDir.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        localDirKeyPressed(evt);
      }
    });
    jToolBar1.add(localDir);

    javax.swing.GroupLayout localSideLayout = new javax.swing.GroupLayout(localSide);
    localSide.setLayout(localSideLayout);
    localSideLayout.setHorizontalGroup(
      localSideLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(localPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
      .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    localSideLayout.setVerticalGroup(
      localSideLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(localSideLayout.createSequentialGroup()
        .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(localPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE))
    );

    add(localSide);
    localSide.setBounds(20, 80, 280, 320);

    remoteSide.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

    remotePanel.setLayout(new java.awt.GridLayout(1, 1));

    jToolBar2.setFloatable(false);
    jToolBar2.setRollover(true);

    remoteLabel.setText("Folder:");
    jToolBar2.add(remoteLabel);

    remote_cdup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jffile/jfile-up.png"))); // NOI18N
    remote_cdup.setFocusable(false);
    remote_cdup.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    remote_cdup.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    remote_cdup.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        remote_cdupActionPerformed(evt);
      }
    });
    jToolBar2.add(remote_cdup);

    remoteDir.setText("/");
    remoteDir.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        remoteDirKeyPressed(evt);
      }
    });
    jToolBar2.add(remoteDir);

    javax.swing.GroupLayout remoteSideLayout = new javax.swing.GroupLayout(remoteSide);
    remoteSide.setLayout(remoteSideLayout);
    remoteSideLayout.setHorizontalGroup(
      remoteSideLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(remotePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)
      .addComponent(jToolBar2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    remoteSideLayout.setVerticalGroup(
      remoteSideLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(remoteSideLayout.createSequentialGroup()
        .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(remotePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE))
    );

    add(remoteSide);
    remoteSide.setBounds(330, 80, 250, 320);

    javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
    localTree.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
    localTree.setComponentPopupMenu(localFileMenu);
    localTree.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(java.awt.event.MouseEvent evt) {
        localTreeMouseClicked(evt);
      }
    });
    localTreeScroll.setViewportView(localTree);

    add(localTreeScroll);
    localTreeScroll.setBounds(20, 20, 280, 50);

    treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
    remoteTree.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
    remoteTree.setComponentPopupMenu(remoteFileMenu);
    remoteTree.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(java.awt.event.MouseEvent evt) {
        remoteTreeMouseClicked(evt);
      }
    });
    remoteTreeScroll.setViewportView(remoteTree);

    add(remoteTreeScroll);
    remoteTreeScroll.setBounds(330, 20, 250, 46);
  }// </editor-fold>//GEN-END:initComponents

    private void remote_downloadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remote_downloadActionPerformed
      download();
    }//GEN-LAST:event_remote_downloadActionPerformed

    private void local_uploadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_local_uploadActionPerformed
      upload();
    }//GEN-LAST:event_local_uploadActionPerformed

    private void local_mkdirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_local_mkdirActionPerformed
      local_mkdir();
    }//GEN-LAST:event_local_mkdirActionPerformed

    private void local_refreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_local_refreshActionPerformed
      local_ls();
    }//GEN-LAST:event_local_refreshActionPerformed

    private void local_deleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_local_deleteActionPerformed
      local_delete();
    }//GEN-LAST:event_local_deleteActionPerformed

    private void local_renameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_local_renameActionPerformed
      local_rename();
    }//GEN-LAST:event_local_renameActionPerformed

    private void remote_mkdirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remote_mkdirActionPerformed
      remote_mkdir();
    }//GEN-LAST:event_remote_mkdirActionPerformed

    private void remote_refreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remote_refreshActionPerformed
      remote_ls();
    }//GEN-LAST:event_remote_refreshActionPerformed

    private void remote_deleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remote_deleteActionPerformed
      remote_delete();
    }//GEN-LAST:event_remote_deleteActionPerformed

    private void remote_renameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remote_renameActionPerformed
      remote_rename();
    }//GEN-LAST:event_remote_renameActionPerformed

    private void remote_propsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remote_propsActionPerformed
      remote_props();
    }//GEN-LAST:event_remote_propsActionPerformed

    private void remoteTreeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_remoteTreeMouseClicked
      if (evt.getClickCount() != 2) {
        return;
      }
      if (evt.getButton() != 1) {
        return;
      }
      dblclickRemoteTree();
    }//GEN-LAST:event_remoteTreeMouseClicked

    private void localTreeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_localTreeMouseClicked
      if (evt.getClickCount() != 2) {
        return;
      }
      if (evt.getButton() != 1) {
        return;
      }
      dblclickLocalTree();
    }//GEN-LAST:event_localTreeMouseClicked

  private void local_cutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_local_cutActionPerformed
    local_cut();
  }//GEN-LAST:event_local_cutActionPerformed

  private void local_copyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_local_copyActionPerformed
    local_copy();
  }//GEN-LAST:event_local_copyActionPerformed

  private void local_pasteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_local_pasteActionPerformed
    local_paste();
  }//GEN-LAST:event_local_pasteActionPerformed

  private void remote_cutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remote_cutActionPerformed
    remote_cut();
  }//GEN-LAST:event_remote_cutActionPerformed

  private void remote_copyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remote_copyActionPerformed
    remote_copy();
  }//GEN-LAST:event_remote_copyActionPerformed

  private void remote_pasteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remote_pasteActionPerformed
    remote_paste();
  }//GEN-LAST:event_remote_pasteActionPerformed

  private void local_openActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_local_openActionPerformed
    local_open("open");
  }//GEN-LAST:event_local_openActionPerformed

  private void remote_openActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remote_openActionPerformed
    remote_open("open");
  }//GEN-LAST:event_remote_openActionPerformed

  private void remote_trashActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remote_trashActionPerformed
    remoteBrowser.trash();
    remote_ls();
  }//GEN-LAST:event_remote_trashActionPerformed

  private void local_trashActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_local_trashActionPerformed
    localBrowser.trash();
    local_ls();
  }//GEN-LAST:event_local_trashActionPerformed

  private void local_propsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_local_propsActionPerformed
    local_props();
  }//GEN-LAST:event_local_propsActionPerformed

  private void local_shareActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_local_shareActionPerformed
    FileEntry files[] = localBrowser.getSelected();
    if ((files == null) || (files.length != 1)) return;
    File share = new File(files[0].file);
    if (!share.exists() || !share.isDirectory()) return;
    FileApp.inDialog = true;
    ShareDialog dialog = new ShareDialog(null, true, share, NetworkShares.getShareName(share.getAbsolutePath()));
    dialog.setVisible(true);
    FileApp.inDialog = false;
    local_ls();
  }//GEN-LAST:event_local_shareActionPerformed

  private void remote_shareActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remote_shareActionPerformed
    FileEntry files[] = remoteBrowser.getSelected();
    if ((files == null) || (files.length != 1)) return;
    File share = new File(files[0].file);
    if (!share.exists() || !share.isDirectory()) return;
    FileApp.inDialog = true;
    ShareDialog dialog = new ShareDialog(null, true, share, NetworkShares.getShareName(share.getAbsolutePath()));
    dialog.setVisible(true);
    FileApp.inDialog = false;
    remote_ls();
  }//GEN-LAST:event_remote_shareActionPerformed

  private void local_editActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_local_editActionPerformed
    local_open("edit");
  }//GEN-LAST:event_local_editActionPerformed

  private void remote_editActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remote_editActionPerformed
    remote_open("edit");
  }//GEN-LAST:event_remote_editActionPerformed

  private void local_mkdir_2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_local_mkdir_2ActionPerformed
    local_mkdir();
  }//GEN-LAST:event_local_mkdir_2ActionPerformed

  private void remote_mkdir_2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remote_mkdir_2ActionPerformed
    remote_mkdir();
  }//GEN-LAST:event_remote_mkdir_2ActionPerformed

  private void local_cdupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_local_cdupActionPerformed
    local_cdup();
  }//GEN-LAST:event_local_cdupActionPerformed

  private void remote_cdupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remote_cdupActionPerformed
    remote_cdup();
  }//GEN-LAST:event_remote_cdupActionPerformed

  private void local_refresh_2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_local_refresh_2ActionPerformed
    localBrowser.refresh();
  }//GEN-LAST:event_local_refresh_2ActionPerformed

  private void remote_refresh_2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remote_refresh_2ActionPerformed
    remoteBrowser.refresh();
  }//GEN-LAST:event_remote_refresh_2ActionPerformed

  private void local_paste_2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_local_paste_2ActionPerformed
    local_paste();
  }//GEN-LAST:event_local_paste_2ActionPerformed

  private void remote_paste_2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_remote_paste_2ActionPerformed
    remote_paste();
  }//GEN-LAST:event_remote_paste_2ActionPerformed

  private void localDirKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_localDirKeyPressed
    if (evt.getModifiers() != 0) return;
    if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
      local_chdir(localDir.getText());
    }
  }//GEN-LAST:event_localDirKeyPressed

  private void remoteDirKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_remoteDirKeyPressed
    if (evt.getModifiers() != 0) return;
    if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
      remote_chdir(remoteDir.getText());
    }
  }//GEN-LAST:event_remoteDirKeyPressed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JToolBar jToolBar1;
  private javax.swing.JToolBar jToolBar2;
  private javax.swing.JPopupMenu localDesktopMenu;
  public javax.swing.JTextField localDir;
  private javax.swing.JPopupMenu localFileMenu;
  private javax.swing.JLabel localLabel;
  private javax.swing.JPanel localPanel;
  private javax.swing.JPanel localSide;
  private javax.swing.JTree localTree;
  private javax.swing.JScrollPane localTreeScroll;
  private javax.swing.JButton local_cdup;
  private javax.swing.JMenuItem local_copy;
  private javax.swing.JMenuItem local_cut;
  private javax.swing.JMenuItem local_delete;
  private javax.swing.JMenuItem local_edit;
  private javax.swing.JMenuItem local_mkdir;
  private javax.swing.JMenuItem local_mkdir_2;
  private javax.swing.JMenuItem local_open;
  private javax.swing.JMenuItem local_paste;
  private javax.swing.JMenuItem local_paste_2;
  private javax.swing.JMenuItem local_props;
  private javax.swing.JMenuItem local_refresh;
  private javax.swing.JMenuItem local_refresh_2;
  private javax.swing.JMenuItem local_rename;
  private javax.swing.JPopupMenu.Separator local_sep_1;
  private javax.swing.JPopupMenu.Separator local_sep_2;
  private javax.swing.JPopupMenu.Separator local_sep_3;
  private javax.swing.JPopupMenu.Separator local_sep_4;
  private javax.swing.JPopupMenu.Separator local_sep_5;
  private javax.swing.JMenuItem local_share;
  private javax.swing.JMenuItem local_trash;
  private javax.swing.JMenuItem local_upload;
  private javax.swing.JPopupMenu remoteDesktopMenu;
  public javax.swing.JTextField remoteDir;
  private javax.swing.JPopupMenu remoteFileMenu;
  private javax.swing.JLabel remoteLabel;
  private javax.swing.JPanel remotePanel;
  private javax.swing.JPanel remoteSide;
  private javax.swing.JTree remoteTree;
  private javax.swing.JScrollPane remoteTreeScroll;
  private javax.swing.JButton remote_cdup;
  private javax.swing.JMenuItem remote_copy;
  private javax.swing.JMenuItem remote_cut;
  private javax.swing.JMenuItem remote_delete;
  private javax.swing.JMenuItem remote_download;
  private javax.swing.JMenuItem remote_edit;
  private javax.swing.JMenuItem remote_mkdir;
  private javax.swing.JMenuItem remote_mkdir_2;
  private javax.swing.JMenuItem remote_open;
  private javax.swing.JMenuItem remote_paste;
  private javax.swing.JMenuItem remote_paste_2;
  private javax.swing.JMenuItem remote_props;
  private javax.swing.JMenuItem remote_refresh;
  private javax.swing.JMenuItem remote_refresh_2;
  private javax.swing.JMenuItem remote_rename;
  private javax.swing.JPopupMenu.Separator remote_sep_1;
  private javax.swing.JPopupMenu.Separator remote_sep_2;
  private javax.swing.JPopupMenu.Separator remote_sep_3;
  private javax.swing.JPopupMenu.Separator remote_sep_4;
  private javax.swing.JPopupMenu.Separator remote_sep_5;
  private javax.swing.JMenuItem remote_share;
  private javax.swing.JMenuItem remote_trash;
  // End of variables declaration//GEN-END:variables
  public XML localFolders = new XML();
  public XML remoteFolders = new XML();
  public int totalFileSize = 0;
  public DefaultTableModel localFilesTableModel = new DefaultTableModel();
  public DefaultTableModel remoteFilesTableModel = new DefaultTableModel();
  public boolean aborted = false;
  public JFileBrowser localBrowser, remoteBrowser;
  private SiteDetails sd;
  public int view;
  private String remoteRoot;  //aka mount point
  private String share;

  private static String getTarget() {
    Random r = new Random();
    String folder;
    do {
      folder = JF.getUserPath() + "/.gvfs/" + Math.abs(r.nextInt()) + "/";
    } while (new File(folder).exists());
    return folder;
  }

  public boolean connect(SiteDetails sd) {
    this.sd = sd;
    if (sd.remoteDir.length() == 0) sd.remoteDir = "/";
    remoteDir.setText(sd.remoteDir);
    if (sd.protocol.equals("local")) {
      remoteRoot = "/";
      remoteBrowser.setPath(sd.remoteDir);
      remote_build_tree();
      remote_ls();
      return true;
    }
    if (sd.protocol.equals("smb")) {
      String initRemoteDir = null;
      int idx = sd.remoteDir.indexOf("/");
      if (idx == 0) {
        initRemoteDir = sd.remoteDir.substring(1);
        idx = initRemoteDir.indexOf("/");
      } else {
        initRemoteDir = sd.remoteDir;
      }
      if (idx == -1) {
        share = initRemoteDir;
        initRemoteDir = "";
      } else {
        share = initRemoteDir.substring(0, idx);
        initRemoteDir = initRemoteDir.substring(idx+1);
      }
      remoteRoot = getTarget();
      String uri = sd.username + "@" + sd.host + "/" + share;
      String args[] = {uri, remoteRoot, "-f"};
      File folder = new File(remoteRoot);
      folder.mkdirs();
      //TODO : send msg to jfDesktop.mount
      //TODO : send passwd
      remoteBrowser.setPath(remoteRoot + initRemoteDir);
      remote_build_tree();
      remote_ls();
      return true;
    }
    if (sd.protocol.equals("ftp")) {
      share = "FTP";
      remoteRoot = getTarget();
      String uri = sd.username + "@" + sd.host;
      String args[] = {uri, remoteRoot, "-f"};
      File folder = new File(remoteRoot);
      folder.mkdirs();
      //TODO : send msg to jfDesktop.mount
      //TODO : send passwd
      remoteBrowser.setPath(remoteRoot + sd.remoteDir);
      remote_build_tree();
      remote_ls();
      return true;
    }
    if (sd.protocol.equals("sftp")) {
      share = "SFTP";
      remoteRoot = getTarget();
      String uri = sd.username + "@" + sd.host;
      String args[] = {uri, remoteRoot, "-f"};
      File folder = new File(remoteRoot);
      folder.mkdirs();
      //TODO : send msg to jfDesktop.mount
      //TODO : send passwd
      remoteBrowser.setPath(remoteRoot + sd.remoteDir);
      remote_build_tree();
      remote_ls();
      return true;
    }
    if (sd.protocol.equals("ftps")) {
      share = "FTPS";
      remoteRoot = getTarget();
      String uri = sd.username + "@" + sd.host;
      String args[] = {uri, remoteRoot, "-f"};
      File folder = new File(remoteRoot);
      folder.mkdirs();
      //TODO : send msg to jfDesktop.mount
      //TODO : send passwd
      remoteBrowser.setPath(remoteRoot + sd.remoteDir);
      remote_build_tree();
      remote_ls();
      return true;
    }
    JFLog.log("Error:Unknown protocol");
    return false;
  }

  public void disconnect() {
    localBrowser.closeFile();  //zip or iso
    localBrowser.close();  //shutdown
    remoteBrowser.closeFile();  //zip or iso
    remoteBrowser.close();  //shutdown
    if (sd == null) return;
    if (!sd.protocol.equals("local")) {
      remoteBrowser.setPath(null);
      //TODO : send msg to jfDesktop.unmount
    }
  }

  public void download() {
    remote_copy();
    local_paste();
  }

  public void upload() {
    local_copy();
    remote_paste();
  }

  public void abort() {
    //TODO
  }

  public static class SiteFileClipboard implements FileClipboard {
    public void get() {
      FileApp.jbusClient.call("org.jflinux.jdesktop." + System.getenv("JID"), "getFileSelection", JBusClient.quote(FileApp.jbusClient.pack));
    }

    public void set(String fileset) {
      FileApp.jbusClient.call("org.jflinux.jdesktop." + System.getenv("JID"), "setFileSelection", JBusClient.quote(JBusClient.encodeString(fileset)));
    }

    public void clear() {
      FileApp.jbusClient.call("org.jflinux.jdesktop." + System.getenv("JID"), "clearFileSelection", "");
    }
  }

  SiteFileClipboard sfc = new SiteFileClipboard();

  public void init(String initLocalDir, boolean localOnly) {
    if (initLocalDir.length() == 0) initLocalDir = JF.getUserPath();
    localDir.setText(initLocalDir.replaceAll("\\\\", "/"));
    localFolders.setTag(localFolders.root, "/", "", "");
    localTree.setModel(localFolders.getTreeModel());

    remoteFolders.setTag(remoteFolders.root, "/", "", "");
    remoteTree.setModel(remoteFolders.getTreeModel());

    localFilesTableModel.addColumn("file");
    localFilesTableModel.addColumn("size");
    localFilesTableModel.addColumn("date");

    remoteFilesTableModel.addColumn("file");
    remoteFilesTableModel.addColumn("size");
    remoteFilesTableModel.addColumn("date");
    remoteFilesTableModel.addColumn("permissions");
    remoteFilesTableModel.addColumn("owner/group");

    view = Settings.settings.defaultView;
    localBrowser = new JFileBrowser(view, initLocalDir, localDesktopMenu
      , localFileMenu, null, 0, false, null, "jfopen", Color.LIGHT_GRAY, Color.BLACK
      , true, false, false, false, FileApp.jbusClient, false, sfc);
    localBrowser.setName("localFiles");  //see FileApp.search()
    localBrowser.setListener(this);
    remoteBrowser = new JFileBrowser(view, null, remoteDesktopMenu
      , localFileMenu, null, 0, false, null, "jfopen", Color.LIGHT_GRAY, Color.BLACK
      , true, false, false, false, FileApp.jbusClient, false, sfc);
    remoteBrowser.setListener(this);
    remoteBrowser.setName("remoteFiles");  //see FileApp.search()

    initGUI(localOnly);
    buildGUI();

    local_build_tree();
    local_ls();

    setState(true);
  }

  public void setView(int view) {
    this.view = view;
    localBrowser.setView(view);
    localBrowser.refresh();
    remoteBrowser.setView(view);
    remoteBrowser.refresh();
  }

  public XML.XMLTag localTag;  //current folder in XML tree

  private void local_build_tree() {
    File root = new File("/");
    File rootFiles[] = root.listFiles();
    for(int a=0;a<rootFiles.length;a++) {
      if (!rootFiles[a].isDirectory()) continue;
      localFolders.addTag(localFolders.root, rootFiles[a].getName(), "", "");
    }
  }

  public void local_ls() {
    localBrowser.refresh();
    String cd = localDir.getText();
    String cds[] = cd.split("/");
    XML.XMLTag tag = localFolders.root;
    for(int a=0;a<cds.length;a++) {
      if (cds[a].length() == 0) continue;
      tag = localFolders.addSetTag(tag, cds[a], "", "");
    }
    localTag = tag;
    localTree.makeVisible(new TreePath(tag.getPath()));
    FileEntry folders[] = localBrowser.getFolders();
    for(int a=0;a<folders.length;a++) {
      localFolders.addSetTag(tag, folders[a].name, "", "");
    }
  }

  public XML.XMLTag remoteTag;

  private void remote_build_tree() {
    File root = new File(remoteRoot);
    File rootFiles[] = root.listFiles();
    if (rootFiles == null) return;
    for(int a=0;a<rootFiles.length;a++) {
      if (!rootFiles[a].isDirectory()) continue;
      remoteFolders.addTag(remoteFolders.root, rootFiles[a].getName(), "", "");
    }
  }

  public void remote_ls() {
    remoteBrowser.refresh();
    String cd = remoteRoot + remoteDir.getText();
    String cds[] = cd.split("/");
    XML.XMLTag tag = remoteFolders.root;
    for(int a=0;a<cds.length;a++) {
      if (cds[a].length() == 0) continue;
      tag = remoteFolders.addSetTag(tag, cds[a], "", "");
    }
    remoteTag = tag;
    remoteTree.makeVisible(new TreePath(tag.getPath()));
    FileEntry folders[] = remoteBrowser.getFolders();
    for(int a=0;a<folders.length;a++) {
      remoteFolders.addSetTag(tag, folders[a].name, "", "");
    }
  }

  public void local_chdir(String path) {
    String wd = localDir.getText();
    path = path.replaceAll("\\\\", "/");
    if (path.equals(".")) {
      //do nothing
    } else if (path.equals("..")) {
      //remove last path element
      int idx = wd.lastIndexOf('/');
      if (idx == -1) {
        wd = "/";
      } else {
        if (JF.isWindows() && wd.length() == 3) {
          wd = "/";
        } else {
          if (idx == 0) {
            wd = "/";
          } else {
            wd = wd.substring(0, idx);
          }
        }
        if (JF.isWindows()) {
          if (wd.length() == 2) {
            wd += "/";
          }
        }
      }
    } else {
      if ((path.length() == 2) && (path.charAt(1) == ':')) {
        wd = path + "/";
      } else {
        if (path.indexOf("/") != -1) {
          //absolute path
          wd = path;
        } else {
          //add path element
          if (JF.isWindows() && wd.equals("/")) {
            wd = path + "/";
          } else {
            if (!wd.endsWith("/")) {
              wd += "/";
            }
            wd += path;
          }
        }
      }
    }
    File folder = new File(wd);
    if (!folder.exists() || !folder.isDirectory()) {
      JFAWT.showError("Error", "Can not find:" + wd);
      localDir.setText(localBrowser.getPath());
      return;
    }
    localDir.setText(wd);
    localBrowser.setPath(wd);
    local_ls();
  }

  public void remote_chdir(String path) {
    String wd = remoteDir.getText();
    path = path.replaceAll("\\\\", "/");
    if (path.equals(".")) {
      //do nothing
    } else if (path.equals("..")) {
      //remove last path element
      int idx = wd.lastIndexOf('/');
      if (idx == -1) {
        wd = "/";
      } else {
        if (JF.isWindows() && wd.length() == 3) {
          wd = "/";
        } else {
          if (idx == 0) {
            wd = "/";
          } else {
            wd = wd.substring(0, idx);
          }
        }
        if (JF.isWindows()) {
          if (wd.length() == 2) {
            wd += "/";
          }
        }
      }
    } else {
      if ((path.length() == 2) && (path.charAt(1) == ':')) {
        wd = path + "/";
      } else {
        if (path.indexOf("/") != -1) {
          //absolute path
          wd = path;
        } else {
          //add path element
          if (JF.isWindows() && wd.equals("/")) {
            wd = path + "/";
          } else {
            if (!wd.endsWith("/")) {
              wd += "/";
            }
            wd += path;
          }
        }
      }
    }
    File folder = new File(remoteRoot + wd);
    if (!folder.exists() || !folder.isDirectory()) {
      JFAWT.showError("Error", "Can not find:" + wd);
      remoteDir.setText(remoteBrowser.getPath());
      return;
    }
    remoteDir.setText(wd);
    remoteBrowser.setPath(remoteRoot + wd);
    remote_ls();
  }

  public void setState(boolean state) {
    localDir.setEnabled(state);
    remoteDir.setEnabled(state);
    localTree.setEnabled(state);
    localBrowser.setEnabled(state);
    remoteTree.setEnabled(state);
    remoteBrowser.setEnabled(state);
  }

  private int cnt=0;

  public void local_mkdir() {
    FileApp.inDialog = true;
    String fn = JFAWT.getString("Enter new folder name", "");
    FileApp.inDialog = false;
    if ((fn == null) || (fn.length() == 0)) {
      return;
    }
    if (fn.indexOf(":") != -1) {
      return;
    }
    if (fn.indexOf("/") != -1) {
      return;
    }
    if (fn.indexOf("?") != -1) {
      return;
    }
    if (fn.indexOf("*") != -1) {
      return;
    }
    String wd = localDir.getText();
    if (!wd.endsWith("/")) {
      wd += "/";
    }
    try {
      _mkdir(wd + fn);
      localFolders.addSetTag(localTag, fn, "", "");
      local_ls();
    } catch (Exception e) {
      FileApp.inDialog = true;
      JFAWT.showError("Error", "Create folder failed:" + e);
      FileApp.inDialog = false;
    }
  }

  public void _mkdir(String path) {
    File file = new File(path);
    file.mkdir();
  }

  public void remote_mkdir() {
    FileApp.inDialog = true;
    String fn = JFAWT.getString("Enter new folder name", "");
    FileApp.inDialog = false;
    if ((fn == null) || (fn.length() == 0)) {
      return;
    }
    if (fn.indexOf(":") != -1) {
      return;
    }
    if (fn.indexOf("/") != -1) {
      return;
    }
    if (fn.indexOf("?") != -1) {
      return;
    }
    if (fn.indexOf("*") != -1) {
      return;
    }
    String wd = remoteDir.getText();
    if (!wd.endsWith("/")) {
      wd += "/";
    }
    try {
      _mkdir(remoteRoot + wd + fn);
      remoteFolders.addSetTag(remoteTag, fn, "", "");
      remote_ls();
    } catch (Exception e) {
      FileApp.inDialog = true;
      JFAWT.showError("Error", "Create folder failed:" + e);
      FileApp.inDialog = false;
    }
  }

  public void local_delete() {
    FileApp.inDialog = true;
    if (!JFAWT.showConfirm("Delete", "Are you sure you want to delete file(s)?")) {
      FileApp.inDialog = false;
      return;
    }
    FileApp.inDialog = false;
    aborted = false;
    local_delete_files();
    local_ls();
  }

  private void local_delete_files() {
    localBrowser.delete();
  }

  public void remote_delete() {
    FileApp.inDialog = true;
    if (!JFAWT.showConfirm("Delete", "Are you sure you want to delete file(s)?")) {
      FileApp.inDialog = false;
      return;
    }
    FileApp.inDialog = false;
    aborted = false;
    remote_delete_files();
    remote_ls();
  }

  private void remote_delete_files() {
    remoteBrowser.delete();
  }

  public void move_files_to_trash(File files[]) {
    localBrowser.trash();
  }

  private int homePathLength;
  private String homePath;

  public void move_file_to_trash(File file) {
    //move file to ~/.local/share/Trash
    //file must be under homePath
    String pathFile = file.getParentFile().getAbsolutePath();
//    System.out.println("pathFile:" + pathFile);
    if (!pathFile.startsWith(homePath)) return;
    String pathRel = pathFile.substring(homePathLength);
//    System.out.println("pathRel:" + pathRel);
    if (pathRel.length() > 0 && !pathRel.startsWith("/")) return;
    File pathTrash = new File(homePath + "/.local/share/Trash" + pathRel);
    pathTrash.mkdirs();
//    System.out.println("mkdirs:" + pathTrash.getAbsolutePath());
    try {
      JFileBrowser.runCmd(null, new String[] {"jmv", file.getAbsolutePath(), pathTrash.getAbsolutePath()});
//      System.out.println("move:" + file.getAbsolutePath() + " to " + pathTrash.getAbsolutePath());
    } catch (Exception e) {
      JFLog.log(e);
    }
  }

  public void local_rename() {
    FileEntry files[] = localBrowser.getSelected();
    if ((files == null) || (files.length != 1)) {
      return;
    }
    File localFile = new File(files[0].file);
    FileApp.inDialog = true;
    String newName = JFAWT.getString("Rename " + (localFile.isDirectory() ? "Folder" : "File"), files[0].name);
    FileApp.inDialog = false;
    if ((newName == null) || (newName.length() == 0)) {
      return;
    }
    File newFile = new File(localDir.getText() + "/" + newName);
    localFile.renameTo(newFile);
    //update localTree
    for (int a = 0; a < localTag.getChildCount(); a++) {
      XML.XMLTag child = localTag.getChildAt(a);
      if (child.getName().equals(files[0].file)) {
        child.setName(newName);
        break;
      }
    }
    local_ls();
  }

  public void remote_rename() {
    FileEntry files[] = remoteBrowser.getSelected();
    if ((files == null) || (files.length != 1)) {
      return;
    }
    File remoteFile = new File(files[0].file);
    FileApp.inDialog = true;
    String newName = JFAWT.getString("Rename " + (remoteFile.isDirectory() ? "Folder" : "File"), files[0].name);
    FileApp.inDialog = false;
    if ((newName == null) || (newName.length() == 0)) {
      return;
    }
    File newFile = new File(remoteRoot + remoteDir.getText() + "/" + newName);
    remoteFile.renameTo(newFile);
    //update remoteTree
    for (int a = 0; a < remoteTag.getChildCount(); a++) {
      XML.XMLTag child = remoteTag.getChildAt(a);
      if (child.getName().equals(files[0].file)) {
        child.setName(newName);
        break;
      }
    }
    remote_ls();
  }

  private void showPath(String name, Object path[]) {
    System.out.println(name);
    for (int a = 0; a < path.length; a++) {
      System.out.println("path[]=" + path[a]);
    }
  }

  private void dblclickLocalTree() {
    TreePath paths[] = localTree.getSelectionPaths();
    if (paths.length != 1) {
      return;
    }
    XML.XMLTag tag = localFolders.getTag(paths[0]);
    ArrayList<String> pathElements = new ArrayList<String>();
    do {
      if (tag.getName().equals("/")) {
        break;  //top node
      }
      pathElements.add(0, tag.getName());  //insert at top
      tag = tag.getParent();
    } while (tag != null);
    String fullPath = "";
    for (int a = 0; a < pathElements.size(); a++) {
      if (!((fullPath.length() == 0) && (JF.isWindows()))) {
        if (!fullPath.endsWith("/")) {
          fullPath += "/";
        }
      }
      fullPath += pathElements.get(a);
    }
    if (fullPath.equals("")) {
      fullPath = "/";
    }
    local_chdir(fullPath);
  }

  private void dblclickRemoteTree() {
    TreePath paths[] = remoteTree.getSelectionPaths();
    if (paths.length != 1) {
      return;
    }
    XML.XMLTag tag = remoteFolders.getTag(paths[0]);
    ArrayList<String> pathElements = new ArrayList<String>();
    do {
      if (tag.getName().equals("/")) {
        break;  //top node
      }
      pathElements.add(0, tag.getName());  //insert at top
      tag = tag.getParent();
    } while (tag != null);
    String fullPath = "";
    for (int a = 0; a < pathElements.size(); a++) {
      if (!fullPath.endsWith("/")) {
        fullPath += "/";
      }
      fullPath += pathElements.get(a);
    }
    if (fullPath.equals("")) {
      fullPath = "/";
    }
    remote_chdir(fullPath);
  }
  public boolean showRemoteSide, showRemoteTree, showLocalTree;
  public boolean showHidden = false;

  private void initGUI(boolean localOnly) {
    showRemoteSide = !localOnly;
    showRemoteTree = false;
    showLocalTree = false;
    if (localOnly) {
      localFileMenu.remove(local_upload);
      localFileMenu.remove(local_sep_1);
      remoteFileMenu.remove(remote_download);
      remoteFileMenu.remove(remote_sep_1);
    } else {
      localFileMenu.remove(local_sep_5);
      localFileMenu.remove(local_share);
      remoteFileMenu.remove(remote_trash);
      remoteFileMenu.remove(remote_sep_5);
      remoteFileMenu.remove(remote_share);
    }
  }

  private JComponent mainPane;

  public void buildGUI() {
    JSplitPane sl1, sr1, s2 = null;
    //first remove all components
    removeAll();
    localPanel.removeAll();
    remotePanel.removeAll();
    //add components as desired
    if (showLocalTree) {
      sl1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, localTreeScroll, localBrowser);
      sl1.setResizeWeight(0.1);
      localPanel.add(sl1);
    } else {
      localPanel.add(localBrowser);
    }
    if (showRemoteSide) {
      if (showRemoteTree) {
        sr1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, remoteTreeScroll, remoteBrowser);
        sr1.setResizeWeight(0.1);
        remotePanel.add(sr1);
      } else {
        remotePanel.add(remoteBrowser);
      }
      s2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, localSide, remoteSide);
      s2.setResizeWeight(0.5);
    }
    if (showRemoteSide) {
      add(s2);
      mainPane = s2;
    } else {
      add(localSide);
      mainPane = localSide;
    }
    setLayout(new GridLayout(1, 1));
    validate();
    repaint();
    localBrowser.requestFocus();
  }

  public void local_cut() {
    localBrowser.cut();
  }

  public void local_copy() {
    localBrowser.copy();
  }

  public void local_paste() {
    localBrowser.paste();
    //refresh both sides
    local_ls();
    remote_ls();
  }

  public void remote_cut() {
    remoteBrowser.cut();
  }

  public void remote_copy() {
    remoteBrowser.copy();
  }

  public void remote_paste() {
    remoteBrowser.paste();
    //refresh both sides
    local_ls();
    remote_ls();
  }

  public void local_open(String action) {
    localBrowser.invoke(localBrowser.getSelected()[0].button);
  }

  public void remote_open(String action) {
    remoteBrowser.invoke(remoteBrowser.getSelected()[0].button);
  }

  public void local_props() {
    FileEntry file = localBrowser.getSelected()[0];
    JFileProperties dialog = new JFileProperties(file, false);
    dialog.setVisible(true);
    if (dialog.value == -1) return;
    try {
      local_ls();
    } catch (Exception e) {
      JFLog.log(e);
    }
  }

  public void remote_props() {
    FileEntry file = remoteBrowser.getSelected()[0];
    JFileProperties dialog = new JFileProperties(file, false);
    dialog.setVisible(true);
    if (dialog.value == -1) return;
    try {
      remote_ls();
    } catch (Exception e) {
      JFLog.log(e);
    }
  }

  public Point getLocalLocation() {
    return localBrowser.getLocationOnScreen();
  }

  public Point getRemoteLocation() {
    return remoteBrowser.getLocationOnScreen();
  }

  public boolean searchLocal(String str) {
    FileEntry files[] = localBrowser.getAll();
    for(int a=0;a<files.length;a++) {
      if (files[a].name.toLowerCase().startsWith(str)) {
        localBrowser.selectFile(files[a]);
        return true;
      }
    }
    return false;
  }

  public boolean searchRemote(String str) {
    FileEntry files[] = remoteBrowser.getAll();
    for(int a=0;a<files.length;a++) {
      if (files[a].name.toLowerCase().startsWith(str)) {
        remoteBrowser.selectFile(files[a]);
        return true;
      }
    }
    return false;
  }

  public void browserResized(JFileBrowser browser) {
    browser.refresh();
  }

  public void browserChangedPath(JFileBrowser browser, String newpath) {
    if (newpath == null) return;
    if (browser == localBrowser) {
      localDir.setText(newpath);
    }
    if (browser == remoteBrowser) {
      if (newpath.length() < remoteRoot.length()) return;
      remoteDir.setText(newpath.substring(remoteRoot.length()-1));
    }
  }

  public void rescan() {
    //media has changed, make sure current path is still valid
    if (!new File(localBrowser.getPath()).exists()) {
      localBrowser.setPath(JF.getUserPath());
    }
    if (!new File(remoteBrowser.getPath()).exists()) {
      remoteBrowser.setPath(JF.getUserPath());
    }
  }

  private JComponent getFocusContainer() {
    JComponent c = (JComponent)FileApp.This.getFocusOwner();
    if (c == null) return null;
    if (c instanceof JButton) {
      return (JComponent)c.getParent();
    }
    return c;
  }

  public JFileBrowser getFocusBrowser() {
    JComponent c = (JComponent)getFocusContainer();
    if (c == localBrowser) return localBrowser;
    if (c == remoteBrowser) return remoteBrowser;
    return null;
  }

  public JTextField getFocusFolder() {
    JComponent c = (JComponent)FileApp.This.getFocusOwner();
    if (c == remoteTree) return remoteDir;
    if (c == remoteDir) return remoteDir;
    if (c == remotePanel) return remoteDir;
    return localDir;
  }

  public boolean isFocusBrowser() {
    JComponent c = (JComponent)getFocusContainer();
    if (c == localBrowser) return true;
    if (c == remoteBrowser) return true;
    return false;
  }

  public void local_cdup() {
    String path = localDir.getText();
    if (path.equals(File.separator)) return;
    int idx = path.lastIndexOf(File.separatorChar);
    path = path.substring(0, idx);
    if (path.length() == 0) path = File.separator;
    localBrowser.setPath(path);
  }

  public void remote_cdup() {
    String path = remoteDir.getText();
    if (path.equals("/")) return;
    int idx = path.lastIndexOf("/");
    path = path.substring(0, idx);
    if (path.length() == 0) path = "/";
    String fullpath = remoteRoot + path;
    fullpath = fullpath.replaceAll("//", "/");
    remoteBrowser.setPath(fullpath);
  }
}
